﻿// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>MyCompany.MyProject</Solution>
// <Project>MyCompany.MyProject.Application</Project>
// <FileName>ShoppingCartAppService.cs</FileName>
// <CreateTime>2017-04-23 23:34</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-23 23:34" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Runtime.Session;
using Abp.UI;
using Vecen.BangChuang.BangChuang.Mall.Dtos;
using Vecen.BangChuang.BangChuang.Products;
using Vecen.BangChuang.BangChuang.Products.Dto;

namespace Vecen.BangChuang.BangChuang.Mall {
    /// <summary>
    /// 购物车操作
    /// </summary>
    [RemoteService(false)]
    public class ShoppingCartAppService: BangChuangAppServiceBase, IShoppingCartAppService {
        private readonly IRepository<ShoppingCart> _shoppingCartRepository;
        private readonly IRepository<Product> _productRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shoppingCartRepository"></param>
        /// <param name="productRepository"></param>
        public ShoppingCartAppService(IRepository<ShoppingCart> shoppingCartRepository,
            IRepository<Product> productRepository) {
            _shoppingCartRepository = shoppingCartRepository;
            _productRepository = productRepository;
        }

        /// <summary>
        /// 获得当前用户购物车
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<ShoppingCartOutDto>> GetShoppingCartAsync() {
            var entities = _shoppingCartRepository.GetAll().Where(c => c.UserId == AbpSession.UserId).ToList();
            var list = entities.MapTo<List<ShoppingCartOutDto>>();
            foreach(var cartOutDto in list) {
                var productEntity = await _productRepository.FirstOrDefaultAsync(cartOutDto.ProductId);
                cartOutDto.Product = productEntity.MapTo<ProductDetailOutDto>();
            }

            return new ListResultDto<ShoppingCartOutDto>(list);
        }

        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddToCartAsync(AddToCartRequestDto input) {
            var product = await _productRepository.FirstOrDefaultAsync(input.ProductId);
            if(product == null || product.Status == ProductStatus.不可售) {
                throw new UserFriendlyException("请求失败!", "商品不存在或已下架!");
            }
            var shoppingCart =
                await _shoppingCartRepository.FirstOrDefaultAsync(
                    c => c.UserId == AbpSession.UserId && c.ProductId == input.ProductId);
            if(shoppingCart == null) {
                shoppingCart = new ShoppingCart();
            }
            shoppingCart.ProductId = input.ProductId;
            shoppingCart.UserId = AbpSession.GetUserId();
            shoppingCart.Count += input.Count;
            shoppingCart.PriceOfTheTime = product.ExFactoryPrice;
            await _shoppingCartRepository.InsertOrUpdateAndGetIdAsync(shoppingCart);
        }

        /// <summary>
        /// 从购物车删减商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeleteAsync(DelFromCartRequestDto input) {
            var cart = await _shoppingCartRepository.FirstOrDefaultAsync(c => c.ProductId == input.ProductId);
            if(cart == null) {
                throw new UserFriendlyException("请求失败!", "购物车已不存在该商品");
            }
            if(input.Count.HasValue) {
                cart.Count -= input.Count.Value;
            } else {
                cart.Count = 0;
            }
            if(cart.Count <= 0) {
                await _shoppingCartRepository.DeleteAsync(cart);
            }
        }
    }
}